﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StudentFieldRepository.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains StudentFieldRepository class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using EntityFrameworkExtensions;

namespace iBuproModel.Repositories.Classes
{
    using System.Collections.Generic;
    using System.Linq;

    using iBuproModel.Repositories.Interfaces;

    /// <summary>
    /// Implements IStudentFieldRepository interface using linq to entities.
    /// </summary>
    public class StudentFieldRepository : GenericRepository<StudentField>, IStudentFieldRepository
    {
        public IEnumerable<StudentField> GetStudentFieldsForStudentPlan(int id)
        {
            return context.StudentFields.Where(f => f.StudentPlan.Id == id).ToList();
        }


        public IEnumerable<StudentField> GetStudentFieldsForStudentPlan(StudentPlan studentPlan)
        {
            return GetStudentFieldsForStudentPlan(studentPlan.Id);
        }


        public void UpdateSubjectList(StudentField studentField, IEnumerable<int> ids)
        {
            studentField.Subjects.Clear();

            foreach (int id in ids)
            {
                studentField.Subjects.Add(context.Subjects.First(s => s.Id == id));
            }
        }

        public void AddStudentFieldToStudentPlan(StudentField studentField, StudentPlan parrent)
        {
            AddStudentFieldToStudentPlan(studentField, parrent.Id);
        }

        public void AddStudentFieldToStudentPlan(StudentField studentField, int studentPlanId)
        {
            context.StudentPlans.FirstOrDefault(p => p.Id == studentPlanId).StudentFields.Add(studentField);
        }

        /*Rewrites base method. New method selects only visible members. This is because of deleting*/
        new public IQueryable<StudentField> GetAll()
        {
            return context.CreateObjectSet<StudentField>().OfTypeOnly<StudentField>().Where(f => f.Visible == true);
        }

        public void Hide(int id)
        {
            StudentField member = this.First(f => f.Id == id);
            member.Visible = false;
            this.SaveChanges();

        }
    }
}
